package com.micro.frog.job.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.frog.core.common.PageResult;
import com.micro.frog.core.contants.MaGlobal;
import com.micro.frog.job.dao.JobLogMapper;
import com.micro.frog.job.model.dto.JobLogDTO;
import com.micro.frog.job.model.entity.JobLog;
import com.micro.frog.job.model.vo.TaskLogVO;
import com.micro.frog.job.service.JobLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author :  ljh
 * @date : 2018/5/8
 **/
@Service
public class JobLogServiceImpl implements JobLogService {
    @Autowired
    JobLogMapper jobLogMapper;

    @Override
    public List<JobLog> all() {
        return jobLogMapper.selectByCondition(null);
    }

    @Override
    public PageResult listPage(JobLogDTO pageVO) {
        pageVO.init();
        Page page = PageHelper.startPage(pageVO.getPageNum(), pageVO.getPageSize(), true);
        JobLog log = new JobLog();
        BeanUtils.copyProperties(page, log);
        List<JobLog> list = jobLogMapper.selectByCondition(log);
        PageResult result = new PageResult();
        result.setCode(MaGlobal.SUCCESS);
        result.setTotal(page.getTotal());
        result.setData(list);
        return result;
    }

    @Override
    public int add(JobLog jobLog) {
        return jobLogMapper.insertSelective(jobLog);
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public int update(JobLog jobLog) {
        return 0;
    }

    @Override
    public TaskLogVO selectLatest(Integer taskId) {
        JobLog log = new JobLog();
        log.setTaskId(taskId);
        List<JobLog> logs = jobLogMapper.selectByCondition(log);
        if(!CollectionUtils.isEmpty(logs)){
            TaskLogVO taskLogVO = new TaskLogVO();
            BeanUtils.copyProperties(logs.get(0),taskLogVO);
            return taskLogVO;
        }
        return null;
    }
}
